<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class Eyeworx_TimekeepingManager extends Module
{
    private $rb = null;

    public function construct()
    {
        load_css('modules/Utils/GenericBrowser/theme/default.css');
        load_css('modules/Utils/RecordBrowser/theme/View_entry.css');
    }

    public function body()
    {
        if (!$this->isset_module_variable('deleted')) $this->set_module_variable('deleted', false);
        if (!$this->get_module_variable('deleted')) {
            DB::Execute("DELETE from eyeworx_timekeeping_data_1");
            $login_history = DB::GetAll("select concat(con.f_first_name,' ', con.f_last_name), DATE_FORMAT(start_time, '%b %e, %Y %h:%i:%s %p'), DATE_FORMAT(end_time, '%b %e, %Y %h:%i:%s %p'), TIMEDIFF(DATE_FORMAT(end_time, '%h:%i:%s %p'), DATE_FORMAT(start_time, '%h:%i:%s %p')) from contact_data_1 con join user_login uli on con.f_login = uli.id join base_login_audit bla on uli.id = bla.user_login_id order by end_time desc");

            $timekeeping_recordset = new RBO_RecordsetAccessor('eyeworx_timekeeping');
            foreach ($login_history as $lh) {
                $r = $timekeeping_recordset->new_record();
                $r->contact = $lh[0];
                $r->time_in = $lh[1];
                $r->time_out = $lh[2];
                $r->duration = $lh[3];
                $r->save();
            }
            $this->set_module_variable('deleted', true);
        }

        $timekeeping_recordset = new Eyeworx_TimekeepingManager_RBO_Timekeeping();
        $this->display_module($timekeeping_recordset->create_rb_module($this));
    }

    public function caption()
    {
        return 'User - Login History';
    }

}